/*
 * Copyright (C) 2012 scchang <scchang@caltech.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ogame.reader;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import ogame.utilities.Constants;
import ogame.utilities.planets.Planet;

/**
 *
 * @author scchang <scchang@caltech.edu>
 */
public class OgameMultiPlanetReader extends JPanel implements ActionListener{
    protected LinkedList<OgameMultiPlanetReaderListener> listeners;
    protected JTextArea inputArea;
    protected JButton parseButton;
    protected OgamePlanetReader planetReader;
    
    public OgameMultiPlanetReader(){
        this.setPreferredSize(new Dimension(Constants.READER_SIZE_WIDTH, 
                Constants.READER_SIZE_HEIGHT));
        this.setFocusable(true);
        
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        this.setLayout(gridbag);
        
        c.gridwidth = GridBagConstraints.RELATIVE;
        
        this.inputArea = new JTextArea(Constants.READER_TEXT_ROWS, 
                Constants.READER_TEXT_COLS);
        this.add(new JScrollPane(this.inputArea), c);
        
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = GridBagConstraints.REMAINDER;
        
        this.parseButton = new JButton("Read");
        this.parseButton.addActionListener(this);
        this.add(this.parseButton, c);
        
        this.listeners = new LinkedList();
        
        this.planetReader = new OgamePlanetReader();
    }
    
    public void actionPerformed(ActionEvent e){
        System.out.println("Multi Read Requested.");
        String txt = this.inputArea.getText();
        if(txt == null){
            return;
        }
        
        Planet[] planet = null;
        planet = this.parseInputArea(txt);
        this.notifyOgameReaderListeners(planet);
    }
    
    protected Planet[] parseInputArea(String txt){
        ArrayList<Planet> planets = new ArrayList<>();
        // Start Splicing
        int last_index = 0;
        while(true){
            int start = txt.indexOf("Resources", last_index);
            start = txt.indexOf("Resources", start+1);
            if(start == -1){
                break;
            }
            int end = txt.indexOf("counter-espionage", start);
            if(end == -1){
                System.out.println("Found a start but not an end.");
                break;
            }
            last_index = start;
            String substring = txt.substring(start, end);
            
            if(substring.indexOf("Defense") == -1){
                // No defenses, lets not select
                continue;
            }
            
            Planet planet = this.planetReader.parseInputArea(substring);
            planets.add(planet);
        }
        
        Planet[] arrayForm = new Planet[planets.size()];
        arrayForm = planets.toArray(arrayForm);
        return arrayForm;
    }
    
    protected void notifyOgameReaderListeners(Planet[] planet){
        for(OgameMultiPlanetReaderListener listener: this.listeners){
            listener.ogameReadPerformed(planet);
        }
    }
    
    public void addOgameReaderListener(OgameMultiPlanetReaderListener in){
        this.listeners.add(in);
    }
    
    public void removeOgameReaderListener(OgameMultiPlanetReaderListener in){
        this.listeners.remove(in);
    }
    
    public void removeOgameReaderListener(int index){
        this.listeners.remove(index);
    }
    
    public void clearOgameReaderListeners(){
        this.listeners.clear();
    }
    
    public LinkedList<OgameMultiPlanetReaderListener> getListeners(){
        return (LinkedList<OgameMultiPlanetReaderListener>)(this.listeners.clone());
    }
}
